//
//  NSString+Pinyin.m
//  FastDoctor
//
//  Created by 李通 on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "NSString+Pinyin.h"

@implementation NSString (Pinyin)

- (NSString *)transformToPinyin {
    
    NSMutableString *mutableString = [NSMutableString stringWithString:self];
    
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    
    mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    
    mutableString = [[mutableString stringByReplacingOccurrencesOfString:@" " withString:@""]mutableCopy];
    
    return mutableString.lowercaseString.copy;
}

- (NSString *)transformToPinyinFirstLetter {
    
    NSMutableString *stringM = [NSMutableString string];
    
    NSString *temp = nil;
    
    for (int i = 0; i < [self length]; i++) {
        
        temp = [self substringWithRange:NSMakeRange(i, 1)];
        
        NSMutableString *mutableString = [NSMutableString stringWithString:temp];
        
        CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
        mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
        
        mutableString = [[mutableString substringToIndex:1]mutableCopy];
        
        [stringM appendString:(NSString *)mutableString];
    }
    return stringM.lowercaseString.copy;
}

@end
